package mes

import (
	"context"
	"encoding/json"
	"ez/apps/mes/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ProcessTemplate struct {
	mgo.BaseDoc  `bson:"-" json:"-"`
	Id           int64            `json:"id" bson:"id,omitempty"`
	Name         string           `json:"name" bson:"name"`                 //模板名称
	Processes    []*Process       `json:"processes" bson:"processes"`       //包含工序
	ProcessesIds []int64          `json:"processesIds" bson:"processesIds"` //包含工序
	Checks       []*CheckTemplate `json:"checks" bson:"checks"`             //检验环节
	ChecksIds    []int64          `json:"checksIds" bson:"checksIds"`       //检验环节
	CreateAt     time.Time        `json:"createAt" bson:"createAt"`
	UpdateAt     time.Time        `json:"updateAt" bson:"updateAt"`
	DeleteAt     *time.Time       `json:"deleteAt" bson:"deleteAt"`
}

func (this *ProcessTemplate) DocName() string { return "ProcessTemplate" }
func (this *ProcessTemplate) GetId() int64    { return this.Id }
func (this *ProcessTemplate) SetId(id int64)  { this.Id = id }
func (this *ProcessTemplate) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ProcessTemplate) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ProcessTemplate) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ProcessTemplate) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ProcessTemplate) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ProcessTemplate) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ProcessTemplate) ToString() string {
	return string(this.ToBytes())
}
func (this *ProcessTemplate) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ProcessTemplate) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ProcessTemplate) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ProcessTemplate) LoadProcesses() {
	if this.Processes == nil {
		this.Processes = make([]*Process, 0)
	}
	if this.ProcessesIds == nil {
		this.ProcessesIds = make([]int64, 0)
	}
	if len(this.ProcessesIds) > 0 {
		this.Processes = make([]*Process, 0)
		for _, v := range this.ProcessesIds {
			find, e := NewProcessCrud().FindId(v)
			if e == nil {
				this.Processes = append(this.Processes, find)
			}
		}
	}
}
func (this *ProcessTemplate) LoadChecks() {
	if this.Checks == nil {
		this.Checks = make([]*CheckTemplate, 0)
	}
	if this.ChecksIds == nil {
		this.ChecksIds = make([]int64, 0)
	}
	if len(this.ChecksIds) > 0 {
		this.Checks = make([]*CheckTemplate, 0)
		for _, v := range this.ChecksIds {
			find, e := NewCheckTemplateCrud().FindId(v)
			if e == nil {
				this.Checks = append(this.Checks, find)
			}
		}
	}
}
func (this *ProcessTemplate) ClearRelationsBeforeSave() mgo.Doc {
	this.Processes = nil
	this.Checks = nil
	return this
}
func neverUsed_ProcessTemplate() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ProcessTemplateAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ProcessTemplate
	Session *ez.Session
}

func NewProcessTemplateAccessControl(model *ProcessTemplate, action string, session *ez.Session) *ProcessTemplateAccessControl {
	ctrl := &ProcessTemplateAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ProcessTemplateAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
